package cn.lixu._05tran;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author LiXu
 * @date 2022/08/16 0016 15:09:49
 */
public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //1.获取生产者对象
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("tran_hello_group");
        //2.设置nameserver地址值
        consumer.setNamesrvAddr("localhost:9876");
        //3.消费者订阅主题，表示此消费者消费哪个主题的消息，二级分类可以是*
        consumer.subscribe("hello_topic", "hello");
        //4.注册信息监听器
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println(new String(messageExt.getBody(), StandardCharsets.UTF_8));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        //5.启动
        consumer.start();
    }
}
